// custom-tab-bar/index.js
Component({
  data:{
    tabBarArr:[
      {
        "pagePath": "/pages/home/home",
        "text": "首页",
        "icon": "home-o"
      },{
        "pagePath": "/pages/topic/topic",
        "text": "专题",
        "icon": "label-o"
      },{
        "pagePath": "/pages/category/category",
        "text": "分类",
        "icon": "apps-o"
      },{
        "pagePath": "/pages/cart/cart",
        "text": "购物车",
        "icon": "cart-o"
      },{
        "pagePath": "/pages/user/user",
        "text": "我的",
        "icon": "user-o"
      }
    ],
    active: 0,
  },
  methods:{
    onChange(event) { 
      // 判断用户是否进入购物车（登录之后才能进入购物车）
      if(this.data.tabBarArr[event.detail].pagePath==='/pages/cart/cart'){
      // 获取登录状态
      let token = wx.getStorageSync('token');
        // 判断用户是否登录
        if(!token){
          // 提示先进行登录
          wx.showToast({
            title: '请先登录！',
            icon: 'loading',
          })
          // 跳转到用户页面
          setTimeout(()=>{
            wx.switchTab({
              url: '/pages/user/user',
            })
          },1000)
          // 阻止往下进行
          return
        }
      }

      // 路由跳转，tabbar切换
      wx.switchTab({
        url: this.data.tabBarArr[event.detail].pagePath
      })
    },
  }
})
